$(function () {
    /* 初始化加载数据表 */
	$('#operateClassroomDialog').hide();
    initClassroomGridData();
    initOther();
});

function initClassroomGridData() {
    $('#classroomDataGrid').datagrid({
        fitColumns: true,
        fit: true,
        idField: 'Id',
        sortName: 'Id',
        sortOrder: 'desc',
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        url: "classroom/all",
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    showOperateClassroomDialog("新增");
                }
            },
            '-',
            {
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                    gridReload();
                }
            }
        ],
        columns: [
            [
                {field: 'classroomLocation', title: '教室位置', align: 'center', width: 50},
                {field: 'operation', title: '操作', align: 'center', width: 80,
                    formatter: function (value, row, index) {
                        return "<a class='up' onclick='showOperateClassroomDialog(\"修改\", " + index + ");'>修改</a>" +
                            "<a class='re' onclick='showDeleteClassroomDialog(" + index + ");'>删除</a>";
                    }
                }
            ]
        ],
	    rowStyler: function(index,row){  
	        if (index % 2 == 1){  
	            return 'background-color:#FFF8DC;';  
	        }  
	    }, 
        onLoadSuccess: function() {
        	changeStyle();
        }
    });
    setDataGridPager($('#classroomDataGrid'));
	$('#operateClassroomDialog').show();
}

function initOther() {
    extendValiateboxType();
}

function showOperateClassroomDialog(operateName, index) {
    $('#oldIdNo').remove();
    $('#operateClassroomDialog').dialog({
        title: operateName + "教室信息",
        width: 256,
        height: 124,
        modal: true,
        buttons: [
            {
                text: operateName,
                handler: function (params) {
                    operateClassroom(operateName);
                }
            },
            {
                text: '取消',
                handler: function () {
                    $('#operateClassroomDialog').dialog('close');
                }
            }
        ]
    });
    clearOldData();
    if (operateName == '修改') {
        var classroom = getSelectedRow('classroomDataGrid', index);
        $("#id").val(classroom.id);
        $("#classroomLocation").val(classroom.classroomLocation);
        $('#operateClassroomDialog').append("<span id='oldIdNo'>" + classroom.classroomLocation + "</span>");
    }
}

function clearOldData() {
    $("#id").val("");
    $("#classroomLocation").val("");
}

function operateClassroom(operationName) {
    var classroomLocation = $('#classroomLocation');
	if ($.trim(classroomLocation.val()).length == 0) {
		$.messager.alert("警告", "请确保红色*内容不为空！", "warning");
		return;
	}
    var tempUrl = "classroom/add";
    if (operationName === '修改') {
        tempUrl = "classroom/update";
    } else {
        $("#id").val(-999);
    }
	if ($('#oldIdNo').text() != null && $('#oldIdNo').text() == $("#classroomLocation").val()) {
		$.ajax({
            type: 'POST',
            data: {
                "id": $("#id").val(),
                "classroomLocation": $("#classroomLocation").val()
            },
            url: tempUrl,
            dataType: "json",
            cache: false,
            error: function (jqXHR, textStatus, errorThrown) {
                $.messager.alert("操作提示", "数据保存异常:[" + jqXHR.statusText + "]", "error");
            },
            success: function (data) {
                if (true == data.result) {
                    $.messager.alert("操作提示", "教室信息操作完成！", "info", function () {
                        $('#operateClassroomDialog').window("close");
                        gridReload($('#classroomDataGrid'));
                    });
                }
            }
        });
	} else {
		$.ajax({
	        type: 'POST',
	        data: { "classroomLocation": $("#classroomLocation").val() },
	        url: "classroom/exists",
	        dataType: "json",
	        cache: false,
	        success: function (data) {
	            if (true == data.result) {
	        		$.messager.alert("警告", "场地已经存在！", "warning");
	            } else {
	                $.ajax({
	                    type: 'POST',
	                    data: {
	                        "id": $("#id").val(),
	                        "classroomLocation": $("#classroomLocation").val()
	                    },
	                    url: tempUrl,
	                    dataType: "json",
	                    cache: false,
	                    error: function (jqXHR, textStatus, errorThrown) {
	                        $.messager.alert("操作提示", "数据保存异常:[" + jqXHR.statusText + "]", "error");
	                    },
	                    success: function (data) {
	                        if (true == data.result) {
	                            $.messager.alert("操作提示", "教室信息操作完成！", "info", function () {
	                                $('#operateClassroomDialog').window("close");
	                                gridReload($('#classroomDataGrid'));
	                            });
	                        }
	                    }
	                });
	            }
	        }
	    });
	}
}

function showDeleteClassroomDialog(index) {
    $.messager.confirm("警告", "确定选中教室信息？", function (data) {
        if (data) {
        	var classroom = getSelectedRow('classroomDataGrid', index);
        	$.ajax({
        		type: 'POST',
        		data: {
        			"classroomId": classroom.id
        		},
        		url: "classroom/remove",
        		dataType: "json",
        		cache: false,
        		error: function (jqXHR, textStatus, errorThrown) {
        			$.messager.alert("操作提示", "数据保存异常:[" + jqXHR.statusText + "]", "error");
        		},
        		success: function (data) {
        			if (true == data.result) {
        				gridReload($('#classroomDataGrid'));
        			} else {
        				$.messager.alert("操作提示", "删除教室信息异常！");
        			}
        		}
        	});
        }
    });
}

//============================ //
function changeStyle() {
	$('.re').linkbutton({  
	    iconCls: 'icon-remove',
	    plain: true
	});  
	$('.up').linkbutton({  
	    iconCls: 'icon-edit',
	    plain: true
	});  
}